using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
namespace SymbolicComputation
{
    public class CalculusTransform : Transform
    {
        String[] constantNamesInEquation;
        String[] constantNamesInTransform;
        public CalculusTransform()
        {
            Match += new MatchDelegate(CalculusTransform_Match);
        }

        bool CalculusTransform_Match(SyntaxNode transformMe, SyntaxNode matchToMe)
        {
            Boolean algebraMatched = AlgebraTransform.MatchAlgebra(transformMe, matchToMe);
            if (algebraMatched == false)
            {
                return false;
            }
            return MatchCalculus(transformMe, matchToMe);
        }

        public Boolean MatchCalculus(SyntaxNode transformMe, SyntaxNode matchToMe)
        {
            String matchToMeNodeText = matchToMe.GetFullText();
            if (constantNamesInTransform.Contains(matchToMeNodeText) == true)
            {
                //a constant is required by the transform
                String transformMeNodeText = transformMe.GetFullText();
                if (constantNamesInEquation.Contains(transformMeNodeText) == false)
                {
                    return false;
                }
            }
           
            return true;
        }

        public static String TransformCalculusString(String transformMe, String inTransform,String[] inConstantNamesInEquation,String[] inConstantNamesInTransform)
        {
            CalculusTransform transform = new CalculusTransform();
            transform.constantNamesInEquation = inConstantNamesInEquation;
            transform.constantNamesInTransform = inConstantNamesInTransform;
            return transform.TransformString(transformMe, inTransform);
        }

        public SyntaxNode TransformCalculusSyntaxNode(SyntaxNode transformMe, Transform transform, String[] inConstantNamesInEquation, String[] inConstantNamesInTransform)
        {
            return TransformCalculusSyntaxNode(transformMe, transform.matchToMe, transform.outPattern,inConstantNamesInEquation, inConstantNamesInTransform);
        }

        public static SyntaxNode TransformCalculusSyntaxNode(SyntaxNode transformMe, SyntaxNode matchToMe, SyntaxNode outPattern, String[] inConstantNamesInEquation, String[] inConstantNamesInTransform)
        {
            CalculusTransform transform = new CalculusTransform();
            transform.constantNamesInEquation = inConstantNamesInEquation;
            transform.constantNamesInTransform = inConstantNamesInTransform;
            return transform.TransformSyntaxNode(transformMe, matchToMe,outPattern);
        }
    }
}